﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="right">
    <input type="text" id="hidid" style="display: none" />
    <input type="text" id="hidgroupid" style="display: none" />
    <h3>用户分组管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
            <div class="manage_register">
                <p><span>分组名称：</span><input type="text" id="name"></p>
            </div>
            <p class="manage_add">
                <a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3, ViewBag.leftid))'>保存</a>
            </p>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th><span>编号</span></th>
                    <th><span>分组名称</span></th>
                    <th><span>添加时间</span></th>
                    <th><span>操作人</span></th>
                    <th><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
    <!--manage end-->
</div>

<div style="width: 100%; height: 100%; display: none; position: absolute; top: 0px; left: 0px;" id="loadDiv">
    <div id="subLoadDiv">
        <img src="@Url.Content("~/Static/Image/load.gif")" />
    </div>
</div>

<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        createMemu();
        //保存
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var groupid = $("#hidgroupid").val();
                var name = $("#name").val();
                if (id > 0)
                    operate("2", id, name, groupid);
                else
                    operate("1", "0", name);
            }
        });
        //取消
        $("#btnCancel").click(function () {
            reseting();
        });
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            gotoPage();
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }

    function ischeck() {
        var name = $("#name").val();
        if (name == "") {
            alert("分组名称不能空！");
            $("#name").focus();
            return false;
        }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["index"] = pageIndex;
        ajaxdata["size"] = pageSize;
        ajaxdata["weixinid"] = weixinid;

        $.ajax({
            url: "@ST.GetTrendsFile("UserGroupManager/GetUserGroupList")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            success: function (result) {
                if (result != "" && result != null && result != undefined) {
                    var arrData = result.split("~");
                    totalCount = arrData[0];
                    var data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    for (var i = 0; i < d.length; i++) {
                        html += "<tr>";
                        html += getTxtTd(d[i]["rowid"]) + getTxtTd(d[i]["name"]) + getTxtTd(d[i]["createtime"].replace(/T/g, " ")) + getTxtTd(d[i]["createname"]);
                        html += "<td><a class=\"manage_bottom_font blue\" onclick=\"getUserGroupByID('" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3, ViewBag.leftid))'>编辑</a>";
                        html += "<a class=\"manage_bottom_font blue\" onclick=\"tongbuToServer(" + d[i]["id"] + ",'" + d[i]["name"] + "')\">发布</a>";
                        html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["id"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4, ViewBag.leftid))'>删除</a></td>";
                        html += '</tr>';
                    }

                    $("#dvList").html(html);
                    PageCountHtml();
                } else {
                    $("#dvList").html("没有查询到相关数据!");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }

    function showLoading() {
        var margintop = (document.documentElement.clientHeight) / 2 - 20;
        var marginleft = (document.documentElement.clientWidth) / 2 - 20;
        $("#subLoadDiv").css({ "margin-top": margintop, "margin-left": marginleft });
        $("#loadDiv").show();
    }

    function hideLoading() {
        $("#loadDiv").hide();
    }

    function tongbuToServer(id, name) {
        $.ajax({
            url: "@Url.Action("TongbuToServer", "UserGroupManager")",
            type: "post",
            data: { id: id, weixinId: weixinid, name: name },
            beforeSend: function () {
                showLoading();
            },
            complete: function () {
                hideLoading();
            },
            success: function (data) {
                if (data == "1") {
                    alert("发布成功！");
                } else {
                    alert("发布失败！");
                }
            }
        });
    }
    //查询
    function getUserGroupByID(id) {
        $.ajax({
            url: "@ST.GetTrendsFile("UserGroupManager/GetUserGroupListByID")",
            type: "GET",
            async: true,
            data: { id: id },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (result) {
                var data = eval("[" + result + "]");
                var d = data[0];
                if (d != null && d != "" && d != undefined) {
                    $("#hidid").val(id);
                    $("#name").val(d["name"]);
                    $("#hidgroupid").val(d["wx_groupid"]);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }

    //编辑操作
    function operate(op, id, name, groupid) {
        $.ajax({
            url: "@ST.GetTrendsFile("UserGroupManager/UpdateUserGroup")",
            type: "POST",
            async: true,
            data: { op: op, id: id, name: name, groupid: groupid, weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    reseting();
                    gotoPage();
                    alert("操作成功。");
                } else alert("操作失败！");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#name").val("");
        $("#hidgroupid").val("0");
    }

    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>

